import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Example01 {
    public static void main(String[] args) {
        Map<String,String> userInfo=loginUI();
        boolean result=VerifyUserInfo(userInfo);
        System.out.println(result ? "账号密码正确" : "账号或密码错误");
        }
        private static  boolean VerifyUserInfo(Map<String, String> userInfo) {
            boolean flag=false;
        String username=userInfo.get("username");
        String password=userInfo.get("password");
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/demo?serverTimezone=GMT&useSSL=false";
            String user="root";
            String pwd="1015";
            connection= DriverManager.getConnection(url,user,pwd);
            String sql="select * from demo where name=? and password=?";
            preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                flag=true;
                break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(resultSet !=null){
                    resultSet.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            try{
                if(preparedStatement !=null){
                    preparedStatement.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            try{
                if(connection !=null){
                    connection.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return flag;
    }

private static Map<String, String> loginUI(){
    Map<String, String> userInfo=new HashMap<>();
    Scanner input=new Scanner(System.in);
    System.out.println("请输入您的账号: ");
    String username=input.nextLine();
    System.out.println("请输入您的密码: ");
    String password=input.nextLine();
    userInfo.put("username",username);
    userInfo.put("password",password);
    return userInfo;
}
}
